
using System.Text;
using System;
using System.Diagnostics;

namespace Microsoft.Services.TfsAlert.AppCode.NotificationEvents
{
    public partial class BuildCompletionEvent : BaseNotificationEvent
    {
        #region INotificationEvent Members

        public override string GetNotificationMessage()
        {
            StringBuilder stringMessageBuilder = new StringBuilder();

            stringMessageBuilder.AppendLine( "Title: " + this.Title );
            stringMessageBuilder.AppendLine( "Completion Status: " + this.CompletionStatus );
            stringMessageBuilder.AppendLine( "Requested By: " + this.RequestedBy );
            stringMessageBuilder.AppendLine( "Build Configuration: " + this.Configuration );

            if ( 0 == String.Compare( "FAILED", this.CompletionStatus, true ) )
                this.turnLightOn();
            else
                this.turnLightOff();

            return stringMessageBuilder.ToString();
        }

        #endregion

        private void turnLightOn()
        {
            RelayIO relay = null;
            try
            {
                relay = new RelayIO();

                relay.Open();
                relay.TurnRelayOn( 3 );
                relay.Close();
            }
            catch ( Exception ex )
            {
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.AppendFormat( "TfsAlert Errored in turnLightOn: \n{0}", ex.Message );
                Trace.TraceInformation( ex.Message );
            }
        }

        private void turnLightOff()
        {
            RelayIO relay = null;

            try
            {
                relay = new RelayIO();
                relay.Open();
                relay.TurnRelayOff( 3 );
                relay.Close();

            }
            catch ( Exception ex )
            {

                StringBuilder errorMessage = new StringBuilder();
                errorMessage.AppendFormat( "TfsAlert Errored in turnLightOff: \n{0}", ex.Message );
                Trace.TraceInformation( ex.Message );
         
            }
        }
    }
}